class TTsJumpSettings {
    constructor() {
        this.settingsInfo = {
            reverseFlag: 0,
            allowTime: 0,
            limitedTime: 0,
            penScoreFail: 4,
            penScoreRefuse: 4,
            penSecondCorrect: 6,
            penOverTimeSecond: 4,
            penOverTimeScore: 1,
            decSize: 2, //时间小数位
            decCarry: 0, //进位方式 0-截断，1-进位，2-四舍五入
        };
    }
}
class TTsJumpSplit {
    constructor(_core, _code) {
        this.code = _code;
        this.core = _core;
        this.info = {
            irm: "",
            start: 0,
            end: 0,
            pauses: [],
            penFail: 0,
            penResfuse: 0,
            penFail: 0,
            penCorrect: 0,
        };
    }
    get settings() {
        var me = this;
        var res = {};
        try {
            res = me.core.settings[me.code].settingsInfo;
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get times() {
        return this.core.times;
    }
    get startTime() {
        return this.getStartTime();
    }
    get endTime() {
        var me = this;
        var res = 0;
        try {
            if (me.info.end > 0) {
                res = me.info.end;
            } else if (me.startTime == 0) {
                res = 0
            } else {
                res = me.times.nowTm;
            }
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get status() {
        var me = this;
        var res = 0;
        try {
            if (me.startTime > 0) {
                if (me.info.end == 1) {
                    res = 3;
                } else if (me.info.pauses.length > 0 && me.info.pauses[me.info.pauses.length - 1].end == 0) {
                    res = 2;
                } else {
                    res = 1;
                }
            }
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get pauseMs() {
        var me = this;
        var res = 0;
        try {
            if (me.info.pauses.length > 0) {
                me.info.pauses.forEach(p => {
                    var endTm = p.end == 0 ? me.endTime : p.end;
                    res = res + endTm - p.start;
                })
            }
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get netMs() {
        var me = this;
        var res = 0;
        try {
            if (me.startTime > 0) {
                res = me.endTime - me.startTime - me.pauseMs + 1000 * me.info.penCorrect;
                if (res > 1000 * me.settings.limitedTime) {
                    res = 1000 * me.settings.limitedTime
                }
            }
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get curSecond() {
        var me = this;
        var res = 0;
        try {
            var netMs = me.netMs;
            if (me.settings.reverseFlag > 0) {
                netMs = 1000 * me.settings.limitedTime - netMs;
            }
            res = me.core.formatTime_Ms2Second(netMs, me.settings.decCarry, me.settings.decSize);
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get curSecondStr() {
        var me = this;
        var res = '';
        try {
            var curSec = me.curSecond;
            res = me.core.formatTime_StrSecond(curSec, me.settings.decSize)
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get enablePause() {
        var me = this;
        var res = false;
        try {
            res = me.startTime > 0 && me.info.end == 0;
            if (res) {
                if (me.info.pauses.length > 0) {
                    var endPauseTm = me.info.pauses[me.info.pauses.length - 1].end;
                    res = endPauseTm > 0;
                }
            }
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    get enableResume() {
        var me = this;
        var res = false;
        try {
            res = me.startTime > 0 && me.info.end == 0 && me.info.pauses.length > 0 && me.info.pauses[me.info.pauses.length - 1].end == 0;
        } catch (error) {
            console.log(error);
        }
        return res;
    }

    getStartTime() {
        var me = this;
        var res = {};
        try {
            res = me.info.start;
        } catch (error) {
            console.log(error);
        }
        return res;
    }
    reset() {
        var me = this;
        try {
            console.log(me.code, ': reset');
            me.info = {
                irm: "",
                start: 0,
                end: 0,
                pauses: [],
                penFail: 0,
                penResfuse: 0,
                penFail: 0,
                penCorrect: 0,
            };
            me.core.events.onSplitHandler(me, 'reset', me.times.nowTm);
        } catch (error) {
            console.log(error);
        }
    }
    start(acTime) {
        var me = this;
        try {
            if (me.info.start == 0) {
                me.info.start = acTime;
                me.core.events.onSplitHandler(me, 'start', acTime);
            }
        } catch (error) {
            console.log(error);
        }
    }
    switchPause(acTime) {
        var me = this;
        try {
            console.log(me.code, ' : switchPause');
            if (me.startTime > 0 && me.info.end == 0) {
                if (me.info.pauses.length > 0 && me.info.pauses[me.info.pauses.length - 1].end == 0) {
                    me.info.pauses[me.info.pauses.length - 1].end = acTime;
                    me.core.events.onSplitHandler(me, 'pause', acTime);
                } else {
                    me.info.pauses.push({
                        start: acTime,
                        end: 0
                    });
                    me.core.events.onSplitHandler(me, 'resume', acTime);
                }
            }
        } catch (error) {
            console.log(error);
        }
    }
    end(acTime) {
        var me = this;
        try {
            if (me.startTime > 0 && me.info.end == 0) {
                if (me.info.pauses.length > 0 && me.info.pauses[me.info.pauses.length - 1].end == 0) {
                    me.info.pauses[me.info.pauses.length - 1].end == acTime;
                }
                me.info.end = acTime;
                me.core.events.onSplitHandler(me, 'end', acTime);
            }
        } catch (error) {
            console.log(error);
        }
    }
}
class TTimes {
    constructor() {
        this.dev = (new Date()).getTime();
        this.local = (new Date()).getTime();
    }
    get nowTm() {
        var me = this;
        return (new Date()).getTime() - me.local + me.dev;
    }
    setTime(acTm) {
        var me = this;
        try {
            me.dev = acTm;
            me.local = (new Date()).getTime();
        } catch (error) {
            console.log(error)
        }
    }
}
class TtsJump {
    constructor() {
        this.times = new TTimes();
        this.settings = {
            matchType: "",
            t0: new TTsJumpSettings(),
            t1: new TTsJumpSettings(),
            t2: new TTsJumpSettings(),
        };
        this.splits = {
            t0: new TTsJumpSplit(this, 't0'),
            t1: new TTsJumpSplit(this, 't1'),
            t2: new TTsJumpSplit(this, 't2'),
        };
        this.events = {
            onSplitHandler: (split, acCode, acTime) => {}
        };
    };
    get level() {
        var me = this;
        var res = 0;
        try {
            if (me.splits.t0.status > 0) {
                res = 1;
                if (me.splits.t1.status > 0) {
                    res = 2;
                    if (me.splits.t2.status > 0) {
                        res = me.splits.t2.status == 3 ? 4 : 3;
                    }
                }
            }
        } catch (error) {
            console.log(error);
        }
        return res;
    };
    formatTime_Ms2Second(msTime, decCarry, decSize) {
        var me = this;
        var res = 0;
        try {
            var v = 0;
            var fun = ([Math.floor, Math.ceil, Math.round])[decCarry];
            if (decSize == 0) {
                v = fun(msTime * 0.001)
            } else if (decSize == 1) {
                v = fun(msTime * 0.01)
                v = v * 0.1
            } else if (decSize == 2) {
                v = fun(msTime * 0.1)
                v = v * 0.01
            } else if (decSize == 2) {
                v = v * 0.001
            }
            res = v;

        } catch (error) {
            console.log(error);
        }
        return res;
    };
    formatTime_StrSecond(sec, decSize) {
        var me = this;
        var res = '';
        try {
            var v = sec + '';
            if (decSize > 0) {
                var pos = v.indexOf(".");
                var endPos = 0;
                if (pos < 0) {
                    v = v + ".000";
                    endPos = v.length + decSize + 1;
                } else {
                    v = v + "000";
                    endPos = pos + decSize;
                }
            }
            res = v.substring(0, endPos);
        } catch (error) {
            console.log(error);
        }
        return res;
    };
    formatTime_Ms2Clock(msTime) {
        var me = this;
        var res = '';
        try {
            if (msTime > 0) {
                var ms = (msTime % 1000) + '000';
                ms = ms.substring(0, 3);
                var s = Math.floor(0.001 * msTime) % 60;
                s = (s < 10 ? '0' : '') + s;
                var m = Math.floor(0.001 * msTime / 60.0) % 60;
                m = (m < 10 ? '0' : '') + m;
                var y = Math.floor(0.001 * msTime / 3600.0) % 24;
                y = (y < 10 ? '0' : '') + y;
                res = y + ":" + m + ":" + s + "." + ms;
            }
        } catch (error) {
            console.log(error);
        }
        return res;
    };
}
export { TtsJump }